<template>
  <div class="app-container">

		<div class="filter-container">
      <el-select class="filter-item" v-model="query.level" placeholder="请选择">
        <el-option label="全部" value="0" ></el-option>
        <el-option label="矿" value="1" ></el-option>
        <el-option label="区域" value="2" ></el-option>
        <el-option label="地点" value="3" ></el-option>
      </el-select>
      <el-input  style="width: 200px;"   v-model="query.name" placeholder="名称" class="filter-item"  ></el-input>
			<el-button @click="getRecords" type="primary" class="filter-item">查询</el-button>
      <el-button @click="showAdd()" type="primary" class="filter-item">新增</el-button>
      <el-button @click="handleDelete()" type="primary" class="filter-item">删除</el-button>
      <el-button style="margin-left: 10px" @click="getRecords" type="danger" class="filter-item">刷新</el-button>
		</div>

		<el-table v-loading="listLoading"	:data="list"	element-loading-text="Loading"	border	fit	highlight-current-row
              @selection-change="handleSelectionChange"
              v-horizontal-scroll="'always'">
      <el-table-column type="selection" width="55" />
			<el-table-column fixed="left" prop="id" label="ID" min-width="50" />
			<el-table-column prop="name" label="名称" min-width="120" />
			<el-table-column prop="get_belong.name" label="所属" min-width="120" />
      <el-table-column prop="level_name" label="类型" min-width="120" />
			<el-table-column prop="created_at" label="创建时间" min-width="160" />
      <el-table-column fixed="right" label="操作" min-width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        </template>
      </el-table-column>
		</el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.page"
      :page-sizes="[10, 20, 50, 100, 200]"
      :page-size="pagination.pageSize"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total">
    </el-pagination>

    <el-dialog :title="isAdd?'新增':'编辑'" :visible.sync="addDialogFormVisible">
      <el-form :model="form">

        <el-form-item label="类型" label-width="120px">
          <el-radio @input="changeRadio" border v-model="form.level" label="2">新增区域</el-radio>
          <el-radio @input="changeRadio" border v-model="form.level" label="3">新增地点</el-radio>
        </el-form-item>

        <el-form-item label="矿场/区域" label-width="120px">
          <el-select filterable v-model="form.pid" >
            <el-option v-for="item in areaMap" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item style="width: 47%" label="名称" label-width="120px">
          <el-input v-model="form.name" ></el-input>
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { myGet,myPost,exportXls } from '@/utils/myUtils'
import {getToken} from "@/utils/auth";

const urlGetAreaMap = '/guard/area/getAreaMap';
const urlUserLst = '/guard/area/lst';
const urlAddUser = '/guard/area/add';
const urlGetUser = '/guard/area/detail';
const urlDeleteUser = '/guard/area/del';
const urlDownloadUser = '/guard/download';
export default {

    data() {
        return {
          token:"",
					host:process.env.VUE_APP_HOST,
					isAdd:true,
					addDialogFormVisible:false,
          selectId:[],
          areaMap:[],

          query:{
						name:'',
            level:'0'
					},
          form:{
            level:'3',
            name:'',
            id:0,
            pid:0
          },
					pagination:{
						total:0,
						page:1,
						pageSize:50,
					},
					list: [],
					formLoading:false,
					listLoading: true
        }
    },
    methods: {
      onSubmit() {
        this.addDialogFormVisible = true;
        this.isAdd = true
        myPost(urlAddUser,this.form).then(res=>{
          if(res.status){
            this.addDialogFormVisible = false;
            this.$message.success(res.msg);
            this.getRecords()
          }
        })
      },
      showAdd(){
        Object.assign(this.$data.form,this.$options.data().form)
        this.addDialogFormVisible = true
        this.changeRadio('3')
      },

      changeRadio(val){
        let level  = 2;
        if(val=='2'){
          //获取矿
          level = 1
        }else{
          //获取曲区域
          level = 2
        }
        myGet(urlGetAreaMap+'?level='+level).then(res=>{
          if(res.status){
            this.areaMap = []
            let records = res.data.records;
            records.forEach(item=>{
              this.areaMap.push(item)
            })
            this.form.pid = this.areaMap[0].id
          }
        })
      },

      download(){
        const url = this.host+urlDownloadUser+"?downloadType=user"
        exportXls(url);
      },

      reload(){
        Object.assign(this.$data.query,this.$options.data().query)
        Object.assign(this.$data.pagination,this.$options.data().pagination)
        this.getRecords()
      },
			getRecords(){
        this.listLoading = true;
        let params = Object.assign(this.query,this.pagination)
					myGet(urlUserLst,params).then((res) =>{
            this.listLoading = false;
            if(res.status){
              this.list = res.data.records
              this.pagination.total = res.data.total
            }
          })
			},
			changeStatus(row){
        myPost(urlAddUser,{id:row.id, status:row.status}).then(res => {
          if(res.status){
            this.$message.success(res.msg)
          }
        })
			},
			handleEdit(index, row) {
				const id = row.id
				this.isAdd = false
        this.addDialogFormVisible = true;

				myGet(urlGetUser,{id}).then(res=>{
				  if(res.status){
				    const record = res.data.records
            this.form.id = record.id
            this.form.pid = record.pid+''
            this.form.level = record.level+''
            this.form.name = record.name
            this.changeRadio(record.level)
          }
        })
			},

      handleDelete(){
        if(this.selectId.length<=0){
          this.$message.error('请先选择项目！')
          return;
        }
        this.$confirm('确定要删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const id = this.selectId
          myGet(urlDeleteUser,{id}).then(res=>{
            if(res.status){
              this.$message.success(res.msg)
              this.getRecords()
            }
          })
        });
      },

      handleSelectionChange(val) {
        this.selectId = [];
        val.forEach(item=>{
          this.selectId.push(item.id)
        })
      },

			handleSizeChange(val) {
					this.pagination.pageSize = val
					this.pagination.page = 1
					this.getRecords()
			},
			handleCurrentChange(val) {
					this.pagination.page = val
					this.getRecords();
			},
			//多选
			toggleSelection(rows) {
					if (rows) {
					rows.forEach(row => {
							this.$refs.multipleTable.toggleRowSelection(row);
					});
					} else {
							this.$refs.multipleTable.clearSelection();
					}
			}
    },
    mounted() {
      this.getRecords()
      this.token = getToken()
    }
}
</script>

<style>

.table-img{
    width: 50px;
    height: 50px;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
}
.avatar {
    width: 100px;
    height: 100px;
    display: block;
}
</style>


